package com.xjtu.conan.controller;


import com.xjtu.conan.entity.UserInfo;
import com.xjtu.conan.response.AjaxResponse;
import com.xjtu.conan.service.NewsImagesInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * <p>
 * 新闻图片表 前端控制器
 * </p>
 *
 * @author 一只黑猩猩
 * @since 2021-04-19
 */
@CrossOrigin
@RestController
@RequestMapping("/conan/news-images-info")
@Api(value = "新闻对应图片管理模块", description = "这是新闻对应图片信息CRUD的模块")
public class NewsImagesInfoController {

    @Autowired
    private NewsImagesInfoService newsImagesInfoService;

    @ApiOperation(value = "获取全部新闻图片", notes = "获取所有新闻图片的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = false, defaultValue = "1", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页数量", required = false, defaultValue = "10", paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/getAllNewsImages", method = RequestMethod.GET)
    public AjaxResponse getAll(@RequestParam(value = "current") Integer current,
                               @RequestParam(value = "size") Integer size) {
        return AjaxResponse.success(newsImagesInfoService.getAllNewsImageInfo(current, size));
    }

    @ApiOperation(value = "根据id查找新闻图片", notes = "获取根据id查找到的新闻图片")
    @ApiImplicitParam(name = "niid", value = "新闻图片id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getNewsImageById", method = RequestMethod.GET)
    public AjaxResponse getNewsImageById(@RequestParam(name = "niid") Long niid) {
        return AjaxResponse.success(newsImagesInfoService.getNewsImageByid(niid));
    }

    @ApiOperation(value = "根据新闻id查找对应图片", notes = "获取根据新闻id查找到的新闻图片")
    @ApiImplicitParam(name = "nid", value = "新闻id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getNewsImagesBynid", method = RequestMethod.GET)
    public AjaxResponse getNewsImageBynid(@RequestParam(name = "nid") Long nid) {
        return AjaxResponse.success(newsImagesInfoService.getNewsImagesBynid(nid));
    }

    @ApiOperation(value = "添加新闻图片信息", notes = "获取新闻图片信息并且添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "files", value = "新闻图片文件", required = true, paramType = "form", dataType = "file"),
            @ApiImplicitParam(name = "nid", value = "所属新闻id", required = true, paramType = "form", dataType = "Long")
    })
    @RequestMapping(value = "/addNewsImages", method = RequestMethod.POST)
    public AjaxResponse addNewsImages(@RequestParam(value = "files") MultipartFile[] files,
                                      @RequestParam(value = "nid") Long nid,
                                      HttpServletRequest request) throws IOException {
        UserInfo loginUser = (UserInfo) request.getSession().getAttribute("loginUser");
        return AjaxResponse.success("数据库受影响行数->" + newsImagesInfoService.addNewsImages(files, nid, loginUser));
    }

    @ApiOperation(value = "删除新闻图片信息", notes = "从数据库中删除某个新闻图片")
    @ApiImplicitParam(name = "niid", value = "删除新闻图片id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/deleteNewsImage", method = RequestMethod.DELETE)
    public AjaxResponse deleteNewsImage(@RequestParam(value = "niid") Long niid) {
        return AjaxResponse.success("数据库受影响行数->" + newsImagesInfoService.deleteNewsImages(niid));
    }

    @ApiOperation(value = "新闻图片总数量", notes = "获取数据库中新闻图片总数量")
    @RequestMapping(value = "/getTotal", method = RequestMethod.GET)
    public AjaxResponse getTotal() {
        return AjaxResponse.success(newsImagesInfoService.getTotal());
    }

}

